home comics writing pictures archive about

word.java

Language: Java
Last Modified: 2020-06-27 1:58:33 PM UTC
File Size: 2588 bytes
http://www.penguinstew.ca/example/hangman/word.java
public class word {
private letter letters[];
private String catagory;
private int length
;
public word() {
int i;
letter letters[] = new letter[26];
for (i = 0; i < 26 ; i++) {
letters[i] = new letter((char)(i+65));
}
this.length = i;
this.letters = letters;
this.catagory = "none";
}
public word(String name, String catagory ) {
int i;
letter letters[] = new letter[name.length()];
for (i = 0; i < name.length() ; i++) {
letters[i] = new letter(name.charAt(i));
}
this.length = i;
this.letters = letters;
this.catagory = catagory;
}
public letter getLetter(int i) {
return this.letters[i];
}
public String getCatagory() {
return this.catagory;
}
public int getLength(){
return this.length;
}
public boolean tryGuess(char guess) {
boolean good = false;
for (int i = 0 ; i < this.length ; i++) {
if(letters[i].getValue() == Character.toUpperCase(guess)) {
letters[i].setPrint(true);
good = true;
}
}
return good;
}
public boolean testGuess(char guess) {
boolean good = false;
for (int i = 0 ; i < this.length ; i++) {
if(letters[i].getValue() == Character.toUpperCase(guess)) {
if (letters[i].getPrint() == false) {
letters[i].setPrint(true);
good = true;
}
}
}
return good;
}
public void reset(){
for(int i = 0; i< this.length ; i++) {
letters[i].setPrint(false);
}
}
public void print(){
System.out.print(" ");
for (int i = 0 ; i < this.length ; i++) {
if(letters[i].getPrint() == true) {
System.out.print(letters[i].getValue());
System.out.print(" ");
} else {
System.out.print("_ ");
}
}
System.out.println();
System.out.print(" Catagory: ");
System.out.println(this.catagory);
}
public void printNoline() {
System.out.print("Guesses: ");
for (int i = 0 ; i < this.length ; i++) {
if(letters[i].getPrint() == true) {
System.out.print(letters[i].getValue());
System.out.print(" ");
}
}
System.out.println();
}
public void printAll(){
System.out.print(" ");
for (int i = 0 ; i < this.length ; i++) {
System.out.print(letters[i].getValue());
System.out.print(" ");
}
System.out.println();
System.out.print(" Catagory: ");
System.out.println(this.catagory);
}
public boolean test() {
for (int i = 0 ; i < this.length ; i++) {
if(letters[i].getPrint() == false) {
return false;
}
}
return true;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119